/*
 * JDockToolBar Copyright (C) 2010 The JDockToolBar Team
 * This program comes with ABSOLUTELY NO WARRANTY; for details see LICENSE
 * This is free software, and you are welcome to redistribute it
 * under certain conditions; see LICENSE for details.
 */
package com.googlecode.jdocktoolbar.swing.ui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Path2D;

import javax.swing.JComponent;
import javax.swing.plaf.basic.BasicToolBarUI;

import com.googlecode.jdocktoolbar.swing.JDockToolBar;

// TODO: Auto-generated Javadoc
/**
 * The Class DockToolBarUI.
 * 
 * @author Marco Biscaro
 */
public class DockToolBarUI extends BasicToolBarUI {

	/**
	 * Creates the UI for {@link JDockToolBar}.
	 * 
	 * @param c
	 *            the c
	 * @return the dock tool bar UI
	 */
	public static DockToolBarUI createUI(JComponent c) {
		return new DockToolBarUI();
	}

	@Override
	public void paint(Graphics g, JComponent c) {
		Graphics2D g2d = (Graphics2D) g.create();

		Dimension d = c.getSize();
		int w = d.width;
		int h = d.height;

		// a = h * sqrt(3) / 6
		float a = h * 0.288675135f;

		Path2D p = new Path2D.Float();
		p.moveTo(0, h);
		p.lineTo(a, h / 2);
		p.lineTo(w - a, h / 2);
		p.lineTo(w, h);
		p.closePath();

		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);

		g2d.setPaint(new GradientPaint(0, 0, new Color(0, 0, 0, 150), 0, c
				.getHeight(), new Color(0, 0, 0, 50)));
		g2d.fill(p);

		g2d.setColor(Color.GRAY);
		g2d.draw(p);

		g2d.dispose();
	}

	@Override
	public Dimension getPreferredSize(JComponent c) {
		JDockToolBar toolBar = (JDockToolBar) c;
		int w = toolBar.getGap() * 4;
		int h = 0;
		for (Component a : toolBar.getComponents()) {
			Dimension d = a.getPreferredSize();
			w += d.getWidth() + toolBar.getGap();
			h = Math.max(h, (int) d.getHeight());
		}
		h += toolBar.getGap() / 3;
		return new Dimension(w, h);
	}

}
